#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    _camera = new Camera(Vector(50,52,267.6), Normalized(Vector(0,-.1,-1.0)),1.8, 120.0);
    _scene = new Scene("Default",_camera,this);
     CSG* _csg = new CSG(nullptr);
//     _csg->setRoot(new Node(Node::Intersection_NodeType,
//                      new Node(new Sphere("Glass",63,0.0,1,0.0,0,0,Vector(0,0,0),Vector(0.15,0.75,0.15),Vector( 50,8.5,81.6),Vector(0,0,0))),
//                      new Node(new Sphere("Glass",63,0.0,1,0.0,0,0,Vector(0,0,0),Vector(0.75,0.15,0.15),Vector( 50,112.75,81.6),Vector(0,0,0)))
//                           ));
//     _csg->setRoot(new Node(Node::Intersection_NodeType,
//                      new Node(new Sphere("Glass",63,0.9,0,0.1,0,0,Vector(0,0,0),Vector(0.75,0.75,0.75),Vector( 50,8.5,81.6),Vector(0,0,0))),
//                      new Node(new Sphere("Glass",63,0.9,0,0.1,0,0,Vector(0,0,0),Vector(0.75,0.75,0.75),Vector( 50,112.75,81.6),Vector(0,0,0)))
//                           ));

     _csg->setRoot(new Node(Node::Substract_NodeType,
                      new Node(new Sphere("Mirror",16.5,0,1,0,0,0,Vector(0,0,0),Vector(0.75,0.0,0.0),Vector( 27,16.5,47+26.5),Vector(0,0,0))),
                      new Node(new Sphere("Mirror",16.5,0,1,0,0,0,Vector(0,0,0),Vector(0.15,0.75,0.0),Vector( 27,16.5,47),Vector(0,0,0)))
                           ));
    _scene->addShape(_csg);
    _scene->addShape(new Sphere("Back",1e5,0,1,0,0,0,Vector(0,0,0),Vector(.75,.75,.75),Vector( 50,40.8, 1e5),Vector(0,0,0)));
    //_scene->addShape(new Sphere("Mirror",16.5,0,0,1,0,0,Vector(0,0,0),Vector(.75,.75,.75),Vector( 27,16.5,47),Vector(0,0,0)));
    _scene->addShape(new Sphere("Botom white",1e5,0,1,0,0,0,Vector(0,0,0),Vector(.75,.75,.75),Vector( 50, 1e5, 81.6),Vector(0,0,0)));
    _scene->addShape(new Sphere("Front",1e5,0,1,0,0,0,Vector(0,0,0),Vector(.75,.75,.75),Vector( 50,40.8,-1e5+170),Vector(0,0,0)));
    _scene->addShape(new Sphere("Right blue",1e5,0,1,0,0,0,Vector(0,0,0),Vector(.25,.25,.75),Vector(-1e5+99,40.8,81.6),Vector(0,0,0)));
    _scene->addShape(new Sphere("Left red",1e5,0,1,0,0.8,0,Vector(0,0,0),Vector(.75,.25,.25),Vector( 1e5+1,40.8,81.6),Vector(0,0,0)));
    _scene->addShape(new Sphere("Top white",1e5,0,1,0,0,0,Vector(0,0,0),Vector(.75,.75,.75),Vector( 50,-1e5+81.6,81.6),Vector(0,0,0)));
    _scene->addShape(new Sphere("Light",56.5,0,0,1,0,0,Vector(1.0,1.0,1.0),Vector(.75,.75,.75),Vector( 50,130,81.6),Vector(0,0,0)));
    //_scene->addShape(new Sphere("Glass",16.5,0.9,0,0.1,0,0,Vector(0,0,0),Vector(.75,.75,.75),Vector( 73,26.5,78),Vector(0,0,0)));

    _raytracer = new RayTracer(_scene,this);
    connect(_raytracer->renderer(),SIGNAL(sampleComputed(int)),this,SLOT(on_progress(int)));
    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(_raytracer);
    ui->centralWidget->setLayout(layout);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    stop();
}

void MainWindow::on_pushButton_clicked()
{
    if(_raytracer->renderer()->isStarted())
    {
        if(!_raytracer->renderer()->isPaused())
            pause();
        else
            resume();
    }
    else
        start();
}

void MainWindow::on_progress(int value)
{
    ui->progressBar->setValue(value);
}

void MainWindow::on_pushButton_2_clicked()
{
    if(_raytracer->renderer()->isStarted())
        stop();
    else
        start();
}

void MainWindow::pause()
{
    if(_raytracer->renderer()->isStarted())
    {
        if(!_raytracer->renderer()->isPaused())
        {
            _raytracer->renderer()->paused(),
                    ui->pushButton->setText("Resume");
            ui->pushButton->setIcon(QIcon(":/res/control.png"));
        }
    }
}

void MainWindow::resume()
{
    if(_raytracer->renderer()->isStarted())
    {
        if(_raytracer->renderer()->isPaused())
        {
            _raytracer->renderer()->resumed();
            ui->pushButton->setText("Pause");
            ui->pushButton->setIcon(QIcon(":/res/control-pause.png"));
        }
    }
}

void MainWindow::start()
{
    if(!_raytracer->renderer()->isStarted())
    {
        ui->progressBar->setMaximum(ui->horizontalSlider->value() * 4);
        _raytracer->setSamples(ui->horizontalSlider->value());
        _raytracer->setDepth(ui->spinBox->value());
        _raytracer->render();
        ui->pushButton->setText("Pause");
        ui->pushButton->setIcon(QIcon(":/res/control-pause.png"));
        ui->pushButton_2->setEnabled(true);
        ui->groupBox->setEnabled(false);
    }
}

void MainWindow::stop()
{
    if(_raytracer->renderer()->isStarted())
    {
        _raytracer->renderer()->stoped();
        ui->pushButton_2->setEnabled(false);
        ui->pushButton->setIcon(QIcon(":/res/control.png"));
        ui->pushButton->setText("Render");
        ui->progressBar->setValue(0);
        ui->groupBox->setEnabled(true);
    }
}

void MainWindow::on_doubleSpinBox_valueChanged(double arg1)
{
    _raytracer->setGamma(arg1);
    _raytracer->update();
}

void MainWindow::on_doubleSpinBox_2_valueChanged(double arg1)
{
    _raytracer->setLuminosity(arg1);
    _raytracer->update();
}

void MainWindow::on_actionImage_HDR_exr_triggered()
{
    if(_raytracer->renderer()->isStarted())
    {
        pause();
        QString filename = QFileDialog::getSaveFileName(this,
                                                        tr("Enregistrer"),
                                                        "/home",
                                                        tr("Image OpenEXR (*.exr)"));
        if(filename != "")
            _raytracer->renderer()->saveAsEXR(filename);
    }
}

void MainWindow::on_actionImage_triggered()
{
    if(_raytracer->renderer()->isStarted())
    {
        pause();
        QString filename = QFileDialog::getSaveFileName(this,
                                                        tr("Enregistrer"),
                                                        "/home",
                                                        tr("Image Jpeg (*.jpg)"));
        if(filename != "")
            _raytracer->renderer()->saveAsJpeg(filename,_raytracer->gamma(),_raytracer->luminosity());
    }
}
